<template>
  <div class="score">
    <c-section v-if="showTitle" title="项目评分">
      <c-table-box ref="cTableBox" hide-func :setting="false" :table-span-method="spanMethod" :table-height="400" :data="tableData" :table-config="tableConfig" />
    </c-section>
    <c-table-box v-else ref="cTableBox" hide-func :setting="false" :table-span-method="spanMethod" :table-height="400" :data="tableData" :table-config="tableConfig" />
  </div>
</template>
<script>
import { postConfigScoreCriteriaPage, postConstUnitAssessScoreDetail } from '@/api/decorate'
export default {
  name: 'Score',
  props: {
    detail: {
      type: Object,
      default: () => ({})
    },
    editable: {
      type: Boolean,
      default: true
    },
    showTitle: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      tableData: [],
      tableConfig: {
        setting: [
          {
            prop: 'scoreType',
            label: '评估类别'
          },
          {
            prop: 'totalScore',
            label: '总分值'
          },
          {
            prop: 'scoreDesc',
            label: '评估说明'
          },
          {
            prop: 'detailItem',
            label: '考核明细'
          },
          {
            prop: 'grading',
            label: '评分标准'
          },
          {
            prop: 'explain',
            label: '评分说明',
            render: (h, row) => {
              if (this.editable) {
                return h('el-input', {
                  props: {
                    placeholder: '请输入评分说明',
                    clearable: true,
                    value: row.explain
                  },
                  on: {
                    input: v => {
                      this.$set(row, 'explain', v)
                    },
                    change: () => {
                      this.$emit('change', this.tableData)
                    }
                  }
                })
              }
              return h('span', row.explain)
            }
          },
          {
            prop: 'sumScore',
            label: '最终得分',
            render: (h, row) => {
              if (this.editable) {
                return h('c-input-number', {
                  props: {
                    placeholder: '请输入最终得分',
                    clearable: true,
                    value: row.sumScore,
                    float: true
                  },
                  on: {
                    input: v => {
                      this.$set(row, 'sumScore', v)
                    },
                    change: (v) => {
                      if (row.totalScore < v) {
                        this.$message.error('最终得分不得高于评估类别总分值！')
                        this.$set(row, 'sumScore', 0)
                        this.setSumScore(0, row)
                      } else {
                        this.setSumScore(v, row)
                      }
                      this.$emit('change', this.tableData)
                    }
                  }
                })
              }
              return h('span', row.sumScore)
            }
          }
        ]
      }
    }
  },
  watch: {
    detail: {
      handler(v) {
        if (!v.decorationTaskCode || !v.constUnitCode) return
        this.getConstUnitAssessScore(v.decorationTaskCode, v.constUnitCode)
      },
      immediate: true
    },
    watch: {
    }
  },
  methods: {
    setSumScore(v, row) {
      for (const item of this.tableData) {
        if (item.flag === row.flag) {
          this.$set(item, 'sumScore', v)
        }
      }
    },
    handlerConfigData(data) {
      const d = []
      for (const item of data) {
        for (let i = 0; i < (item.configScoreDetailDTOS || []).length; i++) {
          const score = item.configScoreDetailDTOS[i]
          const v = {
            scoreType: item.scoreType,
            totalScore: item.totalScore,
            scoreDesc: item.scoreDesc,
            detailItem: score.detailItem,
            grading: score.grading,
            explain: '',
            sumScore: '',
            raw: item,
            rawItem: score,
            flag: item.id,
            spanRow: 0,
            spanColumn: 1
          }
          if (i === 0) {
            v.spanRow = item.configScoreDetailDTOS.length
          }
          d.push(v)
        }
      }
      return d
    },
    handlerDetailData(data) {
      const d = []
      for (const item of data) {
        for (let i = 0; i < (item.assessDetailsDtos || []).length; i++) {
          const score = item.assessDetailsDtos[i]
          const v = {
            scoreType: item.scoreType,
            totalScore: item.totalScore,
            scoreDesc: item.scoreDesc,
            detailItem: score.detailItem,
            grading: score.grading,
            explain: score.scoreDesc,
            sumScore: item.actualScore,
            raw: item,
            rawItem: score,
            flag: item.id,
            spanRow: 0,
            spanColumn: 1
          }
          if (i === 0) {
            v.spanRow = item.assessDetailsDtos.length
          }
          d.push(v)
        }
      }
      return d
    },
    getConstUnitAssessScore(decorationTaskCode, unitCode) {
      postConfigScoreCriteriaPage().then(res => {
        if (this.editable) {
          this.tableData = this.handlerConfigData(res.data.records)
        }
        if (res.data.records.length === 0 || !unitCode) return
        return postConstUnitAssessScoreDetail(decorationTaskCode, unitCode, res.data.records.map(e => e.id)).then(d => {
          const data = d.data
          if (data.length > 0) {
            this.tableData = this.handlerDetailData(data)
          }
        })
      }).then(() => {
        this.$emit('change', this.tableData)
      })
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      if ([0, 1, 2, 7].includes(columnIndex)) {
        return {
          rowspan: row.spanRow,
          colspan: 1
        }
      }
    },
    validate() {
      for (let i = 0; i < this.tableData.length; i++) {
        const v = this.tableData[i]
        // 36296   【门店装修、验收管理】项目过程评估与项目验收评估的评分说明字段逻辑优化
        // if (v.explain === '' || v.explain === undefined) {
        //   this.$message.warning(`请填写第${i + 1}行评分说明！`)
        //   return false
        // }
        if (!this.$gtZero(v.sumScore)) {
          this.$message.warning(`请填写${v.scoreType}最终得分！`)
          return false
        }
      }
      return true
    },
    genData() {
      if (!this.validate()) return Promise.reject('')
      const t = {}
      for (const item of this.tableData) {
        if (!t[item.flag]) {
          t[item.flag] = {
            actualScore: item.sumScore,
            constUnitAssessDetailDTOS: [],
            decorationTaskCode: item.raw.decorationTaskCode || this.detail.decorationTaskCode,
            scoreCriteriaId: item.raw.scoreCriteriaId || item.raw.id,
            unitCode: item.raw.unitCode || this.detail.constUnitCode
          }
        }
        t[item.flag].constUnitAssessDetailDTOS.push({
          scoreDetailId: item.rawItem.scoreDetailId || item.rawItem.id,
          scoreDesc: item.explain
        })
      }
      return Promise.resolve(Object.values(t))
    }
  }
}
</script>
<style lang="scss" scoped>
.score{
  & /deep/ .el-table__row{
      background-color: #fff !important;
    }
    & /deep/ .hover-row{
      background-color: #fff !important;
    }
    & /deep/ td{
      background-color: #fff !important;
    }
}
</style>
